package com.carleasoft.mps.forum.business.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.forum.business.answer.bean.ForumData;
import com.carleasoft.mps.forum.business.answer.bean.ForumFollow;
import com.carleasoft.mps.forum.business.answer.bean.po.ForumUserPo;
import com.carleasoft.mps.forum.business.answer.bean.vo.ForumFollowVo;
import com.carleasoft.mps.forum.business.answer.mapper.ForumFollowMapper;
import com.carleasoft.mps.forum.business.answer.service.ForumDataService;
import com.carleasoft.mps.forum.business.answer.service.ForumFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zang
 * @since 2023-05-09
 */
@Service
public class ForumFollowServiceImpl extends ServiceImpl<ForumFollowMapper, ForumFollow> implements ForumFollowService {
    @Autowired
    private ForumDataService forumDataService;

    @Override
    public Boolean addForumFollow(ForumFollow forumFollow) {
        List<ForumFollow> list = this.list(new QueryWrapper<ForumFollow>()
                .eq("USER_CODE", forumFollow.getUserCode())
                .eq("FOLLOW_USER", forumFollow.getFollowUser())
        );
        if(list.size()>0){
            return false;
        } else {
            this.save(forumFollow);
            return true;
        }
    }

    @Override
    public void delForumFollow(ForumFollow forumFollow) {
        this.remove(new QueryWrapper<ForumFollow>()
                .eq("USER_CODE", forumFollow.getUserCode())
                .eq("FOLLOW_USER", forumFollow.getFollowUser()));
    }

    @Override
    public Page<ForumFollowVo> pageForumFollow(Page page, ForumUserPo forumUserPo) {
        Page<ForumFollowVo> res = this.getBaseMapper().pageForumFollow(page,forumUserPo);
        for (ForumFollowVo re : res.getRecords()) {
            List<ForumData> list = forumDataService.list(new QueryWrapper<ForumData>()
                    .eq("CREATE_PERSON_CODE", re.getFollowUser()).eq("IS_DELETE", 0)
                    .last("limit 3")
            );
            re.setForumDataList(list);
        }
        return res;
    }
}
